import { List, Button,Typography } from "antd"
import {connect,Dispatch,history} from '@umijs/max'
import useIsDrak from "@/hooks/useIsDrak"
const { Title} = Typography
import React from "react"
import { useModel } from '@umijs/max'
const GroupList:React.FC<{data:API.GoupListType[],back:()=>void,dispatch:Dispatch,userInfo:any,loading:any}> = ({data,back,dispatch,userInfo,loading})=>{
    const isDark = useIsDrak()
    const { initialState,setInitialState } =useModel('@@initialState')
    const handleLogin =async (item:API.GoupListType)=>{
       const res = await dispatch<any>({
        type:'userInfo/login',
        payload: {
          mobile:item.mobile as string,
          password:item.password as string,
          type:1,
          user_id:item.user_id,
          user_pass:item.password as string,
          system_source: 'cat',
        }
       })
       console.log(res)
       const menuList = await Promise.all([res.menuList])
       console.log("res",menuList[0])
       await setInitialState({...initialState,userInfo:res.userInfo})
       history.push('/')
    }

    return <List
    style={{maxHeight:'260px',overflow:'auto'}}
    header={<div className=' flex justify-between items-center'>
        <Title level={5} className="!text-white">选择公司</Title>
        <Button onClick={back} type="text" className="!text-white">上一步</Button>
    </div>}

    bordered={false}
    dataSource={data}
    renderItem={(item) => (
      <>
      <List.Item actions={[
        <Button 
        type="primary"
        size="small"
        loading={loading.effects['userInfo/login']}
        key="list-loadmore-edit"  onClick={()=>handleLogin(item)}>登录</Button>
      ]}>
        <span className=" text-white">{item.name}</span>
      </List.Item>
      
       
      </>
     
    )}
  />
}
export default connect(({userInfo,loading })=>({userInfo,loading })=>({userInfo,loading }))(GroupList) 